package cn.wolfcode.luowowo.website.controller;



import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private IStrategyTemplateSearchService strategySearchService;


    @RequestMapping("detail")
    public String detail(Model model,Long id){
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail",detail);

        //阅读量 + 1
        strategyStatisRedisService.viewNumIncrease(id, 1);
        //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(RedisKeys.STRATEGY_STATIS_VO.join(id.toString()), 1);
        model.addAttribute("vo", strategyStatisRedisService.getStrategyStatisVo(id));

        return "strategy/detail";
    }



    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo" ) StrategyDetailQuery qo){
       //接收目的地id，查询攻略细节
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //获取地区集合后删掉并返回被删掉的数据
        Destination destination = toasts.remove(toasts.size() - 1);
       //面包屑
        model.addAttribute("toasts",toasts);
        //当前地区
        model.addAttribute("dest",destination);
        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }

    //旅游攻略
    @RequestMapping("")
    public String index(Model model){
        //abroadCds 国外
        List<StrategyStatisVO> vo = strategyStatisRedisService.listCommendVo();
        //拆分
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        //unabroadCds 国内
        //国内前10
        for (StrategyStatisVO v : vo) {
            if(v.isIsabroad()){
                //国外
                if(abroadCds.size()<10){
                    abroadCds.add(v);
                }
            }else{
                if(unabroadCds.size()<10){
                    unabroadCds.add(v);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);
        //国外前10

        //hotCds  热门
        List<StrategyStatisVO> vos = strategyStatisRedisService.getHotTop10();
        model.addAttribute("hotCds",vos);
        //themeCds 主题
        List<Map<String, Object>> data = strategyTemplateSearchService.getThemeCommendtop10();
        model.addAttribute("themeCds", data);
        //commends 攻略
        List<StrategyCommend> commend = strategyCommendService.getCommendTop5();
        model.addAttribute("commends",commend);
        //chinas
        List<StatisVO> chinas = strategySearchService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);
        //abroads国外
        List<StatisVO> abroads = strategySearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);

        //themes
        List<StatisVO> themes = strategySearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);
        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") StrategySearchQuery qo){
        //分页查询
        Page page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";

    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        if(userInfo==null){
            throw new LogicException("请先登录!");
        }
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        strategyCommentService.save(comment);
        //评论数 + 1
        strategyStatisRedisService.replyNumIncrease(comment.getDetailId(), 1);
        int replynum = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId()).getReplynum();

        //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(RedisKeys.STRATEGY_STATIS_VO.join(comment.getDetailId().toString()), 1);
        return AjaxResult.SUCCESS.addData(replynum);
    }

    //评论完跳转到评论的页面
    @RequestMapping("/comment")
    public String commentPage(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        //page
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")   //toid 文档id   formid作者id
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }

    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //收藏与取消收藏, 根据返回true/false决定
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());

        //排行加分
        // zadd key 分数  value
        //参数1: value , 之前分析了value就是 vo对象的在redis中key    RedisKeys.STRATEGY_STATIS_VO.join(sid.toString())
        //参数2: 分数
        strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), ret?1:-1);

        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVo(sid));
    }

    @RequestMapping("/strategyThumbup") //点赞
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo==null){
            AjaxResult result = new AjaxResult(false,"请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        //点赞加分
        if(ret){
            strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),1);
        }
        return new AjaxResult(ret,null);
    }
}
